//index.js
//获取应用实例
const app = getApp()
const pathUrl = app.globalData.pathUrl;

Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    loginInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],

    showModalDlg: true, // 实名认证弹窗
    qy: false,
  },
  //事件处理函数
  bindViewTap: function () {

  },

  onLoad: function (e) {
    var that = this;
    this.goDd();
    var userInfo = wx.getStorageSync('userInfo');
    var loginInfo = JSON.parse(wx.getStorageSync('loginInfo'));
    var userId = wx.getStorageSync('userId');
    var telphone = wx.getStorageSync('telphone');
    var sfsm = wx.getStorageSync('sfsm');
    var orderNo = wx.getStorageSync('orderNo');
    var realName = wx.getStorageSync('realName');
    var writNumber = wx.getStorageSync('writNumber');
    var caseNumber = wx.getStorageSync('caseNumber');

    console.log(sfsm)
    that.setData({
      userId: userId,
      telphone: telphone,
      sfsm: sfsm,
      myInfo: loginInfo,
      orderNo: orderNo,
      realName: realName,
      writNumber: writNumber,
      caseNumber: caseNumber
    })
    if (sfsm == '已实名') {
      that.setData({
        myInfo: loginInfo,
        loginInfo: true,
        userInfo: userInfo,
        showModalDlg: false
      })
    } else {
      that.setData({
        myInfo: loginInfo,
        loginInfo: true,
        userInfo: userInfo,
        showModalDlg: true,
        sfsm: '未实名'

      })
    }
    this.get04();
    this.postCa();
    this.orderno();
    this.get03();
    this.download();
    // this.sfUserId();
    this.sfsm();
    this.getlist02();
    this.checkws();
  },
  sanji: function () {
    wx.navigateTo({
      url: '/pages/sanji/sanji',
    })
  },
  download: function () {
    var that = this;
    wx.request({
      url: pathUrl + '/fxy/1/downLoad',
      data: {
        caseNumber: that.data.caseNumber,

      },
      method: 'POST',
      success: function (e) {
        console.log(e)
        console.log(e.data.message)
        wx.downloadFile({
          // 示例 url，并非真实存在
          url: pathUrl + '/' + e.data.message,
          success: function (res) {

            const filePath = res.tempFilePath
            wx.openDocument({
              filePath: filePath,
              success: function (res) {
                console.log('打开文档成功')
              }
            })
          }
        })
      }
    })
  },
  postCa: function () {
    var that = this;
    var userId = that.data.userId;
    wx.request({
      url: pathUrl + '/fxy/1/doRealnameByCA',
      data: {
        userId: userId
      },
      method: 'POST',
      success: function (e) {
        console.log(e)
        // console.log(JSON.parse(e.data.message).resultCode)
      }
    })
  },
  search: function () {
    var that = this;
    var userId = that.data.userId;
    wx.request({
      url: pathUrl + '/fxy/1/realnamesearch',
      data: {
        userId: userId
      },
      method: 'POST',
      success: function (e) {
        console.log(e)
        if (e.data.code != '200') {
          wx.showModal({
            title: '提示',
            content: '您暂未实名认证',
            showCancel: false
          })
          return false
        }
        if (e.data.code == '200') {
          wx.showModal({
            title: '提示',
            content: '您已实名成功',
            showCancel: false
          })
          return false
        }
        console.log(JSON.parse(e.data.message).resultData)
        var a = JSON.parse(e.data.message).resultData;
        var b = a.verifyInfos[0].verifyType;

      }
    })
  },
  checkws: function () {
    var that = this;
    wx.request({
      url: pathUrl + '/fxy/1/viewAgreement',
      data: {
        agreementCode: that.data.orderNo
      },
      method: 'POST',
      success: function (e) {
        console.log(e)
      }
    })
  },

  orderno: function () {
    var that = this;
    wx.request({
      url: pathUrl + '/fxy/1/searchWrit',
      data: {
        writNumber: this.data.writNumber,
        agreementCode: this.data.orderNo
      },
      method: 'POST',
      success: function (e) {
        console.log(e)
        var writStatus = JSON.parse(e.data.message).resultData.writStatus
        // if (writStatus == '10') {
        //   wx.showModal({
        //     title: '提示',
        //     content: '未签署成功',
        //     showCancel: false
        //   })
        // }
        // if (writStatus == '11') {
        //   wx.showModal({
        //     title: '提示',
        //     content: '部分签署',
        //     showCancel: false
        //   })
        // }
        // if (writStatus == '12') {
        //   wx.showModal({
        //     title: '提示',
        //     content: '签署完成',
        //     showCancel: false
        //   })
        // }
        // if (writStatus == '19') {
        //   wx.showModal({
        //     title: '提示',
        //     content: '已废止',
        //     showCancel: false
        //   })
        // }
      }
    })
  },
  getlist02: function () {
    var that = this;
    wx.request({
      url: pathUrl + '/gzgl/fxyGzgl02/list',
      data: {
        query07: that.data.orderNo,
      },
      method: 'GET',
      success: function (e) {
        that.setData({
          list02: e.data.result.records
        })
        wx.request({
          url: pathUrl + '/gzgl/fxyGzgl02/list',
          data: {
            query07: that.data.orderNo,
            query13: that.data.realName
          },
          method: 'GET',
          success: function (e) {
            console.log(e.data.result.records)
            that.setData({
              who: e.data.result.records[0].query27
            })
          }
        })
      }
    })
  },
  goSign: function (e) {
    console.log(e)
    var that = this;
    var realName = that.data.realName;
    var name = e.currentTarget.dataset.realname;
    if (name == realName) {
      wx.navigateTo({
        url: '../notificationbook/notificationbook'
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '对不起您不是该用户，没有权限操作',
        showCancel: false
      })
    }

  },
  goSign1: function (e) {
    console.log(e)
    var that = this;
      wx.navigateTo({
        url: '../notificationbook/notificationbook'
      })
  },
  get04: function () {
    var that = this;
    var telphone = that.data.telphone;
    wx.request({
      url: pathUrl + '/gzgl/fxyGzgl04/list',
      method: 'GET',
      data: {
        query07: that.data.orderNo,
      },
      success: function (res) {
        console.log(res)
        console.log(res.data.result.records);
        var list = res.data.result.records[0];
        console.log(list)
        if (list ==''|| list ==null){
          that.setData({
            qy:false
          })
        }else{
          that.setData({
            qy:true
          })
        }

        wx.setStorageSync('caseNumber', list.query16);
        wx.setStorageSync('writNumber', list.query17);
        console.log(list.query16)
        that.setData({
          status: list.query11
        })
      },
      fail: function (res) { }
    });
  },
  get03: function () {
    var that = this;
    var telphone = that.data.telphone;
    wx.request({
      url: pathUrl + '/gzgl/fxyGzgl03/list',
      method: 'GET',
      data: {
        query07: that.data.orderNo,
      },
      success: function (res) {
        console.log(res)
        that.setData({
          list03: res.data.result.records
        })
        console.log(res.data.result.records);
        var list = res.data.result.records[0];
        console.log(list)
        if (list == '' | list == null) {
          wx.setStorageSync('userId1', '');

        } else {
          wx.setStorageSync('userId1', list.query24);

        }

        // wx.setStorageSync('caseNumber', list.query16);
        // wx.setStorageSync('writNumber', list.query17);
        // console.log(list.query16)
        // that.setData({
        //   status: list.query11
        // })
      },
      fail: function (res) { }
    });
  },
  goadd: function () {
    wx.navigateTo({
      url: '/pages/addr/addr',
    })
  },
  pay: function () {
    wx.navigateTo({
      url: '/pages/pay/pay',
    })
  },
  goDd: function () {
    var that = this;
    var telphone = wx.getStorageSync('telphone');
    wx.request({
      url: pathUrl + '/gzgl/fxyGzgl04/list',
      data: {
        query13: telphone
      },
      method: 'GET',
      success: function (e) {
        console.log(e)
        console.log(e.data.result.records)
        that.setData({
          background: e.data.result.records
        })
      }
    })
  },

  sfsm: function () {
    var sfsm = this.data.sfsm;
    console.log(sfsm)
    if (sfsm == '未实名') {
      wx.navigateTo({
        url: '/pages/realname/realname',
      })
    }
  },
  // },
  // sfUserId: function () {
  //   var userId = wx.getStorageSync('userId');
  //   console.log(userId)
  //   if (userId == null || userId == '') {
  //     wx.navigateTo({
  //       url: '../login/login',
  //     })
  //   }
  // },
  generatContract: function () {
    wx.navigateTo({
      url: '../generatContract/generatContract'
    })
  },
  resultCheck: function () {
    wx.navigateTo({
      url: '../resultCheck/resultCheck'
    })
  },
  code: function () {
    wx.navigateTo({
      url: '../code/code'
    })
  },
  // 个人中心
  goMyPerson: function () {
    wx.navigateTo({
      url: '../my/my'
    })
  },
  // CA认证
  caInfo: function () {
    wx.navigateTo({
      url: '../caInfo/caInfo'
    })
  },
  // 实名认证
  realname: function () {
    wx.navigateTo({
      url: '../realname/realname'
    })
  },
  companyApprove: function () {
    wx.navigateTo({
      url: '../companyApprove/companyApprove',
    })
  },
  companyValidation: function () {
    wx.navigateTo({
      url: '../companyValidation/companyValidation',
    })
  },
  // 登陆
  // goLogin: function() {
  //   this.getUserInfo();

  // }, 

  onGotUserInfo: function (info) {
    var that = this;
    console.log(info)
    if (info.detail.userInfo) {
      wx.login({
        success: res => {
          if (res.code) {
            var code = res.code;
            //var encryptedData = (info.detail.encryptedData);
            //var iv = info.detail.iv;
            var rawData = info.detail.rawData;
            //var signature = info.detail.signature;
            that.login_test(code, rawData);
          } else {
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        }
      })
    } else {
      wx.showModal({
        title: '温馨提示',
        content: '为保证您的正常使用，请进行授权!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击了“返回授权”')
          }
        }
      })
    }
  },
  authUri: '/rest/tokens/authv3',
  login_test: function (code, rawData) {
    wx.request({
      url: pathUrl + this.authUri,
      data: {
        JSCODE: code,
        appCode: 'posshop'
      },
      success: function (res) {
        console.log("data*******" + JSON.stringify(res));
        console.log("JSCODE*******" + code);
        console.log(res);
        // 设置缓存
        wx.setStorageSync('openId', res.data.data);
        wx.setStorageSync('rawData', rawData);
        if (res.data.total == 1) {
          wx.switchTab({
            url: "/pages/index/index"
          })
        } else {
          wx.switchTab({
            url: "/pages/index/index"
          })
          // wx.navigateTo({
          //   url: "/pages/my/bindUsers/bindUsers?username="
          // })
        }
      }

    })
  },
  getUserInfo: function (e) {
    console.log(e)
    var userInfo = e.detail.rawData
    var userInfo = JSON.parse(userInfo)
    console.log(userInfo)
    wx.setStorageSync("userInfo", userInfo)
    var that = this;
    wx.navigateTo({
      url: '../login/login'
    })
    var userInfo = wx.getStorageSync('userInfo')
    that.setData({
      userInfo: userInfo
    })
    // 发送 res.code 到后台换取 openId, sessionKey, unionId

  },
  // 
  goto: function () {
    var that = this;
    var status = that.data.status;
    if (status == '0') {
      wx.navigateTo({
        url: '../notificationbook/notificationbook'
      })
    }
    if (status == '1') {
      wx.navigateTo({
        url: '../code/code'
      })
    }
    else if (status == '2') {
      wx.navigateTo({
        url: '../pay/pay'
      })
    }
    else if (status == '3') {
      wx.navigateTo({
        url: '../signdoc/signdoc'
      })
    } else {
      wx.navigateTo({
        url: '../realname/realname',
      })
    }

  },
  // 收执已阅
  goRead: function () {
    wx.navigateTo({
      url: '../readreceipr/readreceipt'
    })
  },

  //取消实名认证
  btnCancel: function () {
    this.setData({
      showModalDlg: false,
    })
  },
  // 立即实名认证
  btnOk: function () {
    // this.setData({
    //   showModalDlg: false,
    // });
    wx.navigateTo({
      url: '../realname/realname'
    })
  },


})